#include "tcp.h"

Tcp::Tcp(QObject *parent)
    : QObject{parent}
{}

Tcp::~Tcp()
{
    qDebug()<<"tcp distruct";
    qDebug()<<(tcpSocket == nullptr);
    if (tcpSocket) {
        // 关闭 UDP 套接字
        tcpSocket->close();
        delete tcpSocket;
        tcpSocket = nullptr;


    }
}

void Tcp::initSocket()
{
    tcpSocket = new QTcpSocket(this);
    tcpSocket->connectToHost(QHostAddress("172.16.4.203"), 8080);
    //连接信号与槽函数
    connect(tcpSocket,&QTcpSocket::readyRead,this,&Tcp::readPendingDatagrams);
}

void Tcp::send()
{
    tcpSocket->write("Hello, World!", 13);
}

void Tcp::readPendingDatagrams()
{
    qDebug()<<"receive";
    QByteArray temp = tcpSocket->readAll();

    qDebug()<<temp;
}
